$buffer = ComRead(
<Size> , <Timeout> )
Reads data from
the serial port.
Parameters
<Size> Number of bytes to read.
<TimeOut> Milliseconds to wait for specified number of bytes to become available.
Return Value
Returns actual bytes read from the port and is stored in $buffer.
Remarks
- The function will either read the specified size of data from the port or wait
to read the data till the system times out.
Example
; Query modem parameter using the AT commands
:MAIN
answer
; Opens COM1 where the voice modem is expected
; Baud rate is set as 9600
; N81 - No Parity, 8 data bits, 1 Stop bit
if ComOpen("COM1", "9600 N81 none")
; Issue the AT&V command to modem
ComWriteLine("at&v")
; Get the size of data returned by modem
$size = ComReadSize()
; Read the data streamed by the modem
while $size > 0
; Modem is a slow device.
; So wait for 2000 seconds for the data to arrive in the port
$buffer = ComRead($size,2000)
display $buffer
log $buffer
Sleep 1000
; See for pending data size to be read
$size = ComReadSize()
endwhile
; Done. Close the port and quit
ComClose()
endif
hangup
goto MAIN
:ONSYSTEMERROR
log $error
hangup
goto MAIN
-
Given above is a script for querying the modem parameters by issuing at&v
command through the serial port. After connecting the voice modem, AT command
is given to get the modem parameters. Before reading the data, the size of
the data stream is determined using the ComReadSize(). If $size is not empty,
then the specified size of data is read from the serial port, setting system
timeout to 2 seconds. Then the content is displayed and we log the buffer.
This process iterates, until the size of the data to be read falls to zero.
Then we disconnect the connection. On occurrence of a system error, the system
will jump on to ONSYSTEMERROR label. The system will note down the error in
the log file.